<% extend('../layout/index.html') %>

<% block('body', function () { %>
  <el-card class="box-card">
    <div slot="header" class="flex">
      <div class="box-1">典型案例</div>
      <el-link @click="goto('/cases/add')" type="primary">添加</el-link>
    </div>
    
    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="title"
        label="标题">
      </el-table-column>
      <el-table-column
        prop="updated_at"
        label="修改时间">
      </el-table-column>
      <el-table-column
        prop="state"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">已发布</el-tag>
          <el-tag type="info" v-else>未发布</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button @click="goto(`/cases/edit/${scope.row.id}`)" type="text" size="small">编辑</el-button>
          <el-popconfirm
            title="是否确认删除该条记录"
            @confirm="remove(`${scope.row.id}`)"
          >
            <el-button slot="reference"  type="text" size="small">删除</el-button>
          </el-popconfirm>
          
        </template>
      </el-table-column>
    </el-table>
  </el-card>
<% }) %>

<% block('script', function () { %>
<script>
(function () {
  new Vue({
    el: '#app',
    data: {
      tableData: []
    },
    created() {
      this.getData()
    },
    methods: {
      async submit () {
        const { data } = await ajax.post('/admin/cases')
      },
      afterUpload (res) {
        this.imageUrl =  `${location.origin}/${res.path}`
      },
      beforeAvatarUpload () {
        
      },
      goto (url) {
        location.href = url
      },
      async getData () {
        const { data } = await ajax.get('/admin/cases')
        this.tableData = data
      },
      async remove (id) {
        try {
          const data = await ajax.delete(`/admin/cases/${id}`)
          this.getData()
          this.$message.success(MESSAGE.SCUCCESS)
        } catch (error) {
          this.$message.error(MESSAGE.ERROR)
        }
      }
    },
  })
})()
</script>
<% }) %>